/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BussinessObject;

import DataAccessObject.SQLServerDAO;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ngoctoan
 */
public class SanphamBO {

    private SQLServerDAO dao = null;
    public int ID_Sanpham;
    public String Masp;
    public String Tensp;
    public String Id_Danhmuc;
    public double Gianhap;
    public double Giaban;
    public int Soluong;
    public String Donvitinh;
    public String Xuatxu;
    public String Mota;

    public SanphamBO(int ID_Sanpham, String Masp, String Tensp, String Donvitinh, double Gianhap, double Giaban, int Soluong) {
        this.ID_Sanpham = ID_Sanpham;
        this.Masp = Masp;
        this.Tensp = Tensp;
        this.Donvitinh = Donvitinh;
        this.Gianhap = Gianhap;
        this.Giaban = Giaban;
        this.Soluong = Soluong;
    }

    public SanphamBO() {
    }

    public SanphamBO(int ID_Sanpham, String Masp, String Tensp, String Donvitinh, double Giaban, int Soluong) {
        this.ID_Sanpham = ID_Sanpham;
        this.Masp = Masp;
        this.Tensp = Tensp;
        this.Donvitinh = Donvitinh;
        this.Giaban = Giaban;
        this.Soluong = Soluong;
    }

    public ResultSet LayIDsp() {
        try {
            dao = new SQLServerDAO();
            return dao.LayDuLieu("SELECT ID_Sanpham FROM tblSanpham WHERE ID_Sanpham = (SELECT MAX(ID_Sanpham) FROM tblSanpham)");
        } catch (Exception ex) {
            Logger.getLogger(NhaphangBO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /*
     * Get ALL products
     */
    public ResultSet GetAll() {
        try {
            dao = new SQLServerDAO();
            ResultSet result = dao.LayDuLieu("Select ID_Sanpham, Masp, Tensp, Tendm, Giaban, Soluong, Donvitinh, Xuatxu, sp.Mota FROM tblDanhmuc dm JOIN tblSanpham sp ON dm.ID_Danhmuc=sp.Id_Danhmuc");
            return result;
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return null;
    }

    /*
     * Find product
     */
    public ResultSet Find(String tuKhoa) {
        try {
            dao = new SQLServerDAO();
            ResultSet rs = dao.LayDuLieu("SELECT * FROM tblSanpham WHERE Masp LIKE N'%" + tuKhoa + "%' OR Tensp LIKE N'%" + tuKhoa + "%' OR Mota LIKE N'%" + tuKhoa + "%'");
            return rs;
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    /*
     * Insert product
     */
    public int Insert(Object Masp, Object Tensp, Object Madm, Object Giaban, Object Soluong, Object Donvitinh, Object Xuatxu, Object Mota) {
        try {
            dao = new SQLServerDAO();
            Object p[] = new Object[8];
            //System.out.println(row+1);

            p[0] = Masp;
            p[1] = Tensp;
            p[2] = Madm;
            p[3] = Giaban;
            p[4] = Soluong;
            p[5] = Donvitinh;
            p[6] = Xuatxu;
            p[7] = Mota;
            System.out.println(p[1]);
            int kq = dao.UpdateQuery("INSERT INTO tblSanpham VALUES(?,?,?,?,?,?,?)", p);
            return kq;
        } catch (Exception e) {
            System.out.println("1" + e);
        }
        return 0;
    }

    /*
     * Update Product
     */
    public int Update(Object ID_Sanpham, Object Masp, Object Tensp, Object Madm, Object Giaban, Object Soluong, Object Donvitinh, Object Xuatxu, Object Mota) {
        try {
            dao = new SQLServerDAO();
            Object p[] = new Object[9];
            //System.out.println(row+1);

            p[0] = Masp;
            p[1] = Tensp;
            p[2] = Madm;
            p[3] = Giaban;
            p[4] = Soluong;
            p[5] = Donvitinh;
            p[6] = Xuatxu;
            p[7] = Mota;
            p[8] = ID_Sanpham;

            int kq = dao.UpdateQuery("UPDATE tblSanpham SET Masp = ?, Tensp = ?, Id_Danhmuc = ?, Giaban = ?, Soluong = ?, Donvitinh = ?, Xuatxu = ?, Mota = ?  WHERE ID_Sanpham = ?", p);
            return kq;
        } catch (Exception e) {
            System.out.println("1" + e);
        }
        return 0;
    }

    /*
     * Delete product
     */
    public int Delete(Object ID_Sanpham) {
        try {
            dao = new SQLServerDAO();
            Object p[] = new Object[1];
            p[0] = ID_Sanpham;

            int kq = dao.UpdateQuery("DELETE FROM tblSanpham WHERE ID_Sanpham = ?", p);
            return kq;
        } catch (Exception e) {
            System.out.println("1" + e);
        }
        return 0;
    }
}
